﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Telerik.Web.UI;
using KPIS.GERP.GAMS.DAL;
using KPIS.GERP.GAMS.MODEL;
using System.Collections.Generic;

namespace KPIS.GERP.GAMS.APP.App_UI.Forms
{
    public partial class ACM_MAS_Payment_Gernal : System.Web.UI.Page
    {
        Commons.Main main = new Commons.Main();
        private D_ACM_PAYMENT_Gernal d_acm_payment = new D_ACM_PAYMENT_Gernal();
        private D_ACM_MAS_Account d_acm_mas_account = new D_ACM_MAS_Account();
        private M_ACM_PAYMENT_Gernal m_acm_payment = new M_ACM_PAYMENT_Gernal();
        private string strExpSeq = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                UpdateUI1("Load");
                //Session["UserID"] = "555";
                ClearObj();
                LoadFilterToExpenseGroup();
               // LoadFilterToAccountCode();
            }
            //if (hnode_tree.Text != "")
            //{
            //    grdPaymentGernal.DataSource = d_acm_payment.GetListForGrid(Convert.ToInt32(hnode_tree.Text));
            //    grdPaymentGernal.DataBind();
            //}
        }
        protected void btnEdit_Click(object sender, EventArgs e)
        {
            UpdateUI1("Edit");
            EnableObj(true);
        }
        protected void LoadGrdPaymentGernal()
        {
            if (hnode_tree.Text == "")
            {
                grdPaymentGernal.DataSource = d_acm_payment.GetListForGrid(0);
            }
            else
            {
                grdPaymentGernal.DataSource = d_acm_payment.GetListForGrid(Convert.ToInt32(hnode_tree.Text));
                //grdPaymentGernal.DataBind();
            }
        }

        protected void grdPaymentGernal_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
        {
            if (!e.IsFromDetailTable)
            {
                LoadGrdPaymentGernal();
            }
        }
        protected void grdPaymentGernal_ItemCommand(object source, GridCommandEventArgs e)
        {
            if (e.CommandName == "DataEdit")
            {
                cmbAccountCode.Focus();
                UpdateUI1("Update");
                M_ACM_PAYMENT_Gernal info = d_acm_payment.GetListFillBySeq(main.ConvertNullInt(grdPaymentGernal.Items[e.Item.ItemIndex]["bgm_exp_seq"].Text));
                txtExpenseCode.Text = info.bgm_exp_code;
                txtExpenseName.Text = info.bgm_exp_name;
                txtAccountCode.Text = info.account_seq.ToString();
                txtExpense_Descr.Text = info.bgm_exp_descr;
                hbgm_exp_seq.Text = info.bgm_exp_seq.ToString();
                hupper_bgm_exp_seq.Text = info.upper_bgm_exp_seq.ToString();
                M_ACM_MAS_Account infoacc = d_acm_mas_account.GetListFillByAccountSeq(info.account_seq);
                cmbAccountCode.Text = infoacc.account_code_name;
                //cmbAccountCode.SelectedValue = info.account_seq.ToString();
            }
        }
        //protected void LoadFilterToAccountCode()
        //{
        //    cmbAccountCode.DataSource = d_acm_mas_account.GetListAccount();
        //    cmbAccountCode.DataBind();
        //    main.InsertEmptyDDLSelect(cmbAccountCode);
        //}
        protected void cmbAccountCode_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            string strFilter = e.Text;
            IList<M_ACM_MAS_Account> infoList = d_acm_mas_account.GetListDataAccountFilter(strFilter);
            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + 10, infoList.Count);
            e.EndOfItems = endOffset == infoList.Count;

            for (int i = itemOffset; i < endOffset; i++)
            {
                cmbAccountCode.Items.Add(new RadComboBoxItem(infoList[i].account_code_name, infoList[i].account_seq.ToString()));
            }

            e.Message = main.GetStatusMessage(endOffset, infoList.Count);
        }
        protected void cmbExpenseGroup_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            radTreeviewExpense.DataSource = d_acm_payment.GetListForUpperExpense(main.ConvertNullInt(cmbExpenseGroup.SelectedValue));
            radTreeviewExpense.DataBind();
           // PanelData.Visible = true;
            grdPaymentGernal.DataSource = d_acm_payment.GetListForGrid(0);
            grdPaymentGernal.DataBind();
            ClearObj();
        }
        protected void LoadFilterToExpenseGroup()
        {
            cmbExpenseGroup.DataSource = d_acm_payment.GetListForExpenseGroup();
            cmbExpenseGroup.DataBind();
            main.InsertEmptyDDLSelect(cmbExpenseGroup);
        }
        protected void radTreeviewExpense_NodeClick(object sender, Telerik.Web.UI.RadTreeNodeEventArgs e)
        {
           // PanelData.Visible = true;
            string strExpSeq = e.Node.Value;
            hnode_tree.Text = strExpSeq;
            M_ACM_PAYMENT_Gernal info  = d_acm_payment.GetListFillBySeq(main.ConvertNullInt(strExpSeq));
            UpdateUI1("Update");
            txtAccountCode.Text = info.account_code;
            txtExpenseCode.Text = info.bgm_exp_code;
            txtExpenseName.Text = info.bgm_exp_name;
            hbgm_exp_seq.Text = info.bgm_exp_seq.ToString();
            M_ACM_MAS_Account infoacc = d_acm_mas_account.GetListFillByAccountSeq(info.account_seq);
            cmbAccountCode.Text = infoacc.account_code_name;
            //cmbAccountCode.SelectedValue = info.account_seq.ToString();
            //grdPaymentGernal.DataSource = d_acm_payment.GetListForUpperExpense(main.ConvertNullInt(cmbExpenseGroup.SelectedValue));
            //if (hnode_tree.Text == "")
            //{
            //    grdPaymentGernal.DataSource = d_acm_payment.GetListForGrid(0);
            //}
            //else
            //{
            //    grdPaymentGernal.DataSource = d_acm_payment.GetListForGrid(Convert.ToInt32(hnode_tree.Text));
            //    grdPaymentGernal.DataBind();
            //}
            LoadGrdPaymentGernal();
            grdPaymentGernal.DataBind();
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            UpdateUI1("Save");
            EnableObj(false);
            m_acm_payment.bgm_exp_seq = Convert.ToInt32(hbgm_exp_seq.Text);
            m_acm_payment.account_seq = main.ConvertNullInt(cmbAccountCode.SelectedValue);
            //m_acm_payment.updated_by = main.ConvertNullInt(Session["UserID"].ToString());
            //m_acm_payment.updated_when = DateTime.Now;
            d_acm_payment.Update(m_acm_payment);
            //LoadGrdPaymentGernal();
            //grdPaymentGernal.DataSource = d_acm_payment.GetListForUpperExpense(main.ConvertNullInt(cmbExpenseGroup.SelectedValue));
            grdPaymentGernal.DataSource = d_acm_payment.GetListForGrid(main.ConvertNullInt(hupper_bgm_exp_seq.Text));
            grdPaymentGernal.DataBind();
        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            UpdateUI1("Cancel");
            EnableObj(false);
            ClearObj();
        }
        protected void RadAjaxManager1_AjaxRequest(object sender, AjaxRequestEventArgs e)
        {
            string strArg;
            strArg = e.Argument;
            cmbAccountCode.SelectedValue = strArg;
            M_ACM_MAS_Account infoacc = d_acm_mas_account.GetListFillByAccountSeq(Convert.ToInt32(strArg));
            cmbAccountCode.Text = infoacc.account_code_name;
            
        }
        //============================ control events ==============================
        protected void UpdateUI1(string strCommand)
        {
            switch (strCommand)
            {
                case "Load":
                    lblStatus.Text = "แสดงสถานะการทำงาน";
                    main.StrSelectWord = "< เลือกรายการรหัสบัญชี >";
                    btnSave.Enabled = false;
                    btnCancel.Enabled = false;
                    btnAccountSearch.Enabled = false;
                    txtAccountCode.Enabled = false;
                    cmbAccountCode.Enabled = false;
                    txtExpense_Descr.Enabled = false;
                   // PanelData.Visible = false;
                    ClearObj();
                    break;
                case "Update":
                    lblStatus.Text = "แก้ไขข้อมูลจากที่เลือก";
                    btnSave.Enabled = true;
                    btnCancel.Enabled = true;
                    btnAccountSearch.Enabled = true;
                    txtAccountCode.Enabled = true;
                    cmbAccountCode.Enabled = true;
                    txtExpense_Descr.Enabled = true;
                    break;
                case "Save":
                    lblStatus.Text = "บันทึกข้อมูลเรียบร้อยแล้ว";
                    btnSave.Enabled = false;
                    btnCancel.Enabled = false;
                    btnAccountSearch.Enabled = false;
                    txtAccountCode.Enabled = false;
                    cmbAccountCode.Enabled = false;
                    txtExpense_Descr.Enabled = false;
                    break;
                case "Cancel":
                    lblStatus.Text = "ยกเลิกข้อมูล";
                    btnSave.Enabled = false;
                    btnCancel.Enabled = false;
                    btnAccountSearch.Enabled = false;
                    txtAccountCode.Enabled = false;
                    cmbAccountCode.Enabled = false;
                    txtExpense_Descr.Enabled = false;
                    ClearObj();
                    break;
            }
        }
        //============================ Control Object ==============================
        protected Boolean checkValidate()
        {
            Boolean pFlag = true;
            if (cmbAccountCode.Text == "")
            {
                cmbAccountCode.Focus();
                lblStatus.Text = "กรุณากรอกข้อมูลของรหัสบัญชีก่อนบันทึกรายการด้วย";
                pFlag = false;
            }
            if (cmbAccountCode.Text.Trim().Length > 11)
            {
                cmbAccountCode.Focus();
                lblStatus.Text = "กรอกข้อมูลรหัสบัญชีเกิน 11 ตัวกรุณาทำรายการใหม่ด้วย";
                pFlag = false;
            }
            if (txtExpense_Descr.Text.Trim().Length > 255)
            {
                txtExpense_Descr.Focus();
                lblStatus.Text = "ข้อมูลรายละเอียดไม่สามารถกรอกเกิน 255 ตัวอักษรได้";
                pFlag = false;
            }
            return pFlag;
        }
        protected void EnableObj(Boolean pFlag)
        {
            txtExpenseCode.Enabled = pFlag;
            txtExpenseName.Enabled = pFlag;
            cmbAccountCode.Enabled = pFlag;
            txtAccountCode.Enabled = pFlag;
            txtExpense_Descr.Enabled = pFlag;
        }
        protected void ClearObj()
        {
            txtExpenseCode.Text = "";
            txtExpenseName.Text = "";
            cmbAccountCode.SelectedValue = "";
            txtAccountCode.Text = "";
            txtExpense_Descr.Text = "";
            hnode_tree.Text = "";
        }

  
    }

    }

